<?php
/**
 * Created by PhpStorm.
 * User: huangkai
 * Date: 2017/11/20
 * Time: 14:48
 */

namespace Wap\Model;


use Think\Model;

class StatisticsModel extends Model
{
    public static $api_arr = [
        "meeting",
        "yuandan",
        "wnz",
        "lottery",
        "daily_sign"
    ];//---功能块列表---
    public static function visit($api = '', $id = 0)
    {
        $uv = cookie($api . $id . date("Y-m-d"));
        $data = array('pv' => 1, 'uv' => 1);
        if (empty($uv)) {
            $time = strtotime(date("Y-m-d", strtotime("+1 day")) . " 00:00:00") - time();
            cookie($api . $id . date("Y-m-d"), date("Y-m-d"), $time);
            $model = S($api);
            if (isset($model[$id])) {
                $data['pv'] = $model[$id]['pv'] + 1;
                $data['uv'] = $model[$id]['uv'] + 1;
            }
        } else {
            $model = S($api);
            if (isset($model[$id])) {
                $data['pv'] = $model[$id]['pv'] + 1;
                $data['uv'] = $model[$id]['uv'];
            }
        }
        if($api && $id){
            $model = S($api);
            $model[$id] = $data;
            S($api, $model, 172800);
        }
    }
    //---获取当天数据统计信息--
    public static function gettoday($api = '', $topic_id = '')
    {
        $data = array('pv' => 0, 'uv' => 0);
        $api_arr = self::$api_arr;
        //--没有传api--
        if (empty($api)) {
            foreach ($api_arr as $api_each) {
                $s_data_arr = S($api_each);
                foreach ($s_data_arr as $data_val) {
                    $data['pv'] += $data_val['pv'];
                    $data['uv'] += $data_val['uv'];
                }
            }
        } else {//--传了--
            //--设置了此类api--
            if (in_array($api, $api_arr)) {
                $s_data_arr = S($api);
                //--传了topic_id--
                if (!empty($topic_id)) {
                    //--缓存数据中是否包含此topic_id--
                    if (isset($s_data_arr[$topic_id])) {
                        $data['pv'] += $s_data_arr[$topic_id]['pv'];
                        $data['uv'] += $s_data_arr[$topic_id]['uv'];
                    }
                } else {//--没有传topic_id--
                    foreach ($s_data_arr as $data_val) {
                        $data['pv'] += $data_val['pv'];
                        $data['uv'] += $data_val['uv'];
                    }
                }
            }
        }
        return $data;
    }

    //--获取某天统计信息--
    public static function getdate($api = '', $topic_id = '', $start_date = '')
    {
        $data = array('pv' => 0, 'uv' => 0);
        $cond = [];
        if (!empty($api)) {
            $cond['api'] = $api;
        }
        if (!empty($topic_id)) {
            $cond['topic_id'] = $topic_id;
        }
        if (empty($start_date)) {
            $data = self::gettoday($api, $topic_id);
        } else {
            $cond['date'] = $start_date;
            $result = M("statistics")->where($cond)->find();
            if (!empty($result)) {
                $data['pv'] = $result['pv_num'];
                $data['uv'] = $result['uv_num'];
            }
        }
        return $data;
    }

    //--获取某段时间统计信息列表--
    public static function getdaterange_list($api = '', $topic_id = '', $start_date = '', $end_date = '')
    {
        $result = array();
        $cond = [];
        $today = date("Y-m-d");
        if (!empty($api)) {
            $cond['api'] = $api;
        }
        if (!empty($topic_id)) {
            $cond['topic_id'] = $topic_id;
        }
        if (empty($start_date) && empty($end_date)) {
            $start_date = date("Y-m-d", strtotime("-6 day"));
            $end_date = $today;
        }
        $cond['date'] = array("between", $start_date . "," . $end_date);
        $result = M("statistics")->where($cond)->index("date")->getField("date,pv_num,uv_num");
        if ((!$start_date || ($today >= $start_date)) && (!$end_date || ($today <= $end_date))) {
            $data = self::gettoday($api, $topic_id);
            $result[$today] = array("date" => $today, "pv_num" => $data['pv'], "uv_num" => $data['uv']);
        }
        return $result;
    }

    //--获取某月统计信息--
    public static function getmonth($api = '', $topic_id = '', $month = '')
    {
        $data = array('pv' => 0, 'uv' => 0);
        $cond = [];
        if (!empty($api)) {
            $cond['api'] = $api;
        }
        if (!empty($topic_id)) {
            $cond['topic_id'] = $topic_id;
        }
        if (!empty($month)) {
            $cond['month'] = $month;
            if ($month == date("m")) {
                $s_data = self::gettoday($api, $topic_id);
                $data['pv'] += $s_data['pv'];
                $data['uv'] += $s_data['uv'];
            }
        } else {
            $s_data = self::gettoday($api, $topic_id);
            $data['pv'] += $s_data['pv'];
            $data['uv'] += $s_data['uv'];
            $cond['month'] = date("m");
        }
        $pv_num = M("statistics")->where($cond)->sum("pv_num");
        if (!empty($pv_num)) {
            $data['pv'] += $pv_num;
        }
        $uv_num = M("statistics")->where($cond)->sum("uv_num");
        if (!empty($uv_num)) {
            $data['uv'] += $uv_num;
        }
        return $data;
    }

    //--获取某年统计信息--
    public static function getyear($api = '', $topic_id = '', $year = '')
    {
        $data = array('pv' => 0, 'uv' => 0);
        $cond = [];
        if (!empty($api)) {
            $cond['api'] = $api;
        }
        if (!empty($topic_id)) {
            $cond['topic_id'] = $topic_id;
        }
        if (!empty($year)) {
            $cond['year'] = $year;
            if ($year == date("Y")) {
                $s_data = self::gettoday($api, $topic_id);
                $data['pv'] += $s_data['pv'];
                $data['uv'] += $s_data['uv'];
            }
        } else {
            $s_data = self::gettoday($api, $topic_id);
            $data['pv'] += $s_data['pv'];
            $data['uv'] += $s_data['uv'];
            $cond['year'] = date("Y");
        }
        $pv_num = M("statistics")->where($cond)->sum("pv_num");
        if (!empty($pv_num)) {
            $data['pv'] += $pv_num;
        }
        $uv_num = M("statistics")->where($cond)->sum("uv_num");
        if (!empty($uv_num)) {
            $data['uv'] += $uv_num;
        }
        return $data;
    }
}